package brau.view;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.inject.Inject;

import org.primefaces.event.SelectEvent;

import br.gov.frameworkdemoiselle.annotation.NextView;
import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractListPageBean;
import br.gov.frameworkdemoiselle.transaction.Transactional;
import brau.business.EnderecoBC;
import brau.domain.Endereco;

@ViewController
@NextView("/endereco_edit.xhtml")
@PreviousView("/endereco_list.xhtml")
public class EnderecoListMB extends AbstractListPageBean<Endereco, Long> {

	private static final long serialVersionUID = 1L;

	@Inject
	private EnderecoBC bc;
	
	private String filterEndereco = "";
	private List<Endereco> listaEndereco = new ArrayList<Endereco>();
	private Endereco enderecoSelected = new Endereco();
	
	
	
	public EnderecoListMB() {
		super();
	}

	@Override
	protected List<Endereco> handleResultList() {
		return this.bc.findAll();
	}
	
	@Transactional
	public String deleteSelection() {
		boolean delete;
		for (Iterator<Long> iter = getSelection().keySet().iterator(); iter.hasNext();) {
			Long id = iter.next();
			delete = getSelection().get(id);
			if (delete) {
				bc.delete(id);
				iter.remove();
			}
		}
		return getPreviousView();
	}

	
	public String onRowSelectNavigate(SelectEvent event) {  
//      FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedCliente", event.getObject());
		enderecoSelected = (Endereco) event.getObject();
      return "";
  }  


	
	public void pesquisarEndereco() {
		listaEndereco = bc.buscarEnderecoByFilter(filterEndereco);
	}

	public String getFilterEndereco() {
		return filterEndereco;
	}

	public void setFilterEndereco(String filterEndereco) {
		this.filterEndereco = filterEndereco;
	}

	public List<Endereco> getListaEndereco() {
		return listaEndereco;
	}

	public void setListaEndereco(List<Endereco> listaEndereco) {
		this.listaEndereco = listaEndereco;
	}

	public Endereco getEnderecoSelected() {
		return enderecoSelected;
	}

	public void setEnderecoSelected(Endereco enderecoSelected) {
		this.enderecoSelected = enderecoSelected;
	}

	
	
}
